<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <base href="<%=request.getContextPath()%>/">
    <!-- import CSS -->
    <link rel="stylesheet" type="text/css" href="static/css/vue-index.css"/>
</head>
<body>
<div id="app" class="wallet-container">
    <!--搜索区域-->
    <div class="search-area">
        <el-input v-model="searchForm.item" style="width: 15%" size="small" placeholder="事项描述"></el-input>
        <el-input v-model="searchForm.amount" style="width: 12%" size="small" placeholder="金额"></el-input>
        <el-select v-model="searchForm.type" placeholder="交易类型" clearable style="width: 12%; margin-left: 10px"
                   size="small">
            <el-option label="收入" value="收入"></el-option>
            <el-option label="支出" value="支出"></el-option>
        </el-select>
        <el-select v-model="searchForm.category" placeholder="分类" clearable style="width: 12%; margin-left: 10px"
                   size="small">
            <el-option label="餐饮" value="餐饮"></el-option>
            <el-option label="购物" value="购物"></el-option>
            <el-option label="交通" value="交通"></el-option>
            <el-option label="居住" value="居住"></el-option>
            <el-option label="娱乐" value="娱乐"></el-option>
            <el-option label="医疗" value="医疗"></el-option>
            <el-option label="工资" value="工资"></el-option>
            <el-option label="奖金" value="奖金"></el-option>
            <el-option label="日用品" value="日用品"></el-option>
            <el-option label="其他" value="其他"></el-option>
        </el-select>
        <el-button style="margin: 0 5px 0 5px" type="primary" size="small" @click="search">查询</el-button>
        <el-button style="margin: 0 5px 0 5px" type="primary" size="small" @click="reset">重置</el-button>
    </div>
    <!--功能按钮区域-->
    <div class="function-area">
        <el-button size="small" @click="add">新增</el-button>
        <el-button size="small" @click="importFurns">导入</el-button>
        <el-button size="small" @click="exportFurn">导出Excel</el-button>
    </div>
    <!--表格区域-->
    <div class="table-area">
        <el-table :data="tableData" ref="walletTable" style="width: 100%;" height="100%"
                  @row-click="handleRowClick" :cell-style="cellStyle" :header-cell-style="headerCellStyle">
            <el-table-column type="selection" align="center"></el-table-column>
            <el-table-column fixed sortable prop="item" label="事项描述"></el-table-column>
            <el-table-column fixed sortable prop="amount" label="金额"></el-table-column>
            <el-table-column fixed sortable prop="type" label="交易类型"></el-table-column>
            <el-table-column fixed sortable prop="category" label="分类"></el-table-column>
            <el-table-column fixed sortable prop="paymentMethod" label="支付方式"></el-table-column>
            <el-table-column fixed sortable prop="createTime" label="创建时间"></el-table-column>
            <el-table-column fixed="right" label="操作" align="center" width="180">
                <template #default="scope">
                    <div class="operation-links">
                        <a href="javascript:;" class="text-link primary" @click="handleEdit(scope.row)">编辑</a>
                        <span class="divider">|</span>
                        <a href="javascript:;" class="text-link primary" @click="handleView(scope.row)">查看</a>
                        <span class="divider">|</span>
                        <el-popconfirm title="确定删除该交易记录吗？" @confirm="handleDelete(scope.row.id)">
                            <template #reference>
                                <a href="javascript:;" class="text-link danger">删除</a>
                            </template>
                        </el-popconfirm>
                    </div>
                </template>
            </el-table-column>
        </el-table>
    </div>

    <!--分页-->
    <div class="pagination-area">
        <el-pagination
                :current-page.sync="searchForm.pageNum"
                :page-size.sync="searchForm.pageSize"
                :page-sizes="[10, 20, 50, 100]"
                :total="total"
                layout="total, sizes, prev, pager, next, jumper"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange">
        </el-pagination>
    </div>

    <!--家居表单对话框-->
    <el-dialog :visible.sync="dialogVisible"
               :title="dialogType === 'add' ? '新增交易记录' : dialogType === 'edit' ? '编辑交易记录' : '查看交易记录'"
               width="60%">
        <el-form :model="form" :rules="rules" ref="walletForm" label-width="120px" :disabled="dialogType === 'view'">
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item prop="item" label="事项描述">
                        <el-tooltip content="请输入交易事项的详细描述，如购物内容、用餐地点等" placement="top" effect="light">
                            <el-input v-model="form.item" size="small" placeholder="请输入事项描述"></el-input>
                        </el-tooltip>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item prop="amount" label="金额">
                        <el-tooltip content="请输入交易金额，支持两位小数" placement="top" effect="light">
                            <el-input-number v-model="form.amount" :precision="2" :min="0" size="small"
                                             style="width: 100%"></el-input-number>
                        </el-tooltip>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item prop="type" label="交易类型">
                        <el-tooltip content="请选择交易类型：收入或支出" placement="top" effect="light">
                            <el-select v-model="form.type" placeholder="请选择交易类型" style="width: 100%" size="small">
                                <el-option label="收入" value="收入"></el-option>
                                <el-option label="支出" value="支出"></el-option>
                            </el-select>
                        </el-tooltip>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item prop="category" label="分类">
                        <el-tooltip content="请选择交易分类，便于统计和分析" placement="top" effect="light">
                            <el-select v-model="form.category" placeholder="请选择分类" style="width: 100%" size="small">
                                <el-option label="餐饮" value="餐饮"></el-option>
                                <el-option label="购物" value="购物"></el-option>
                                <el-option label="交通" value="交通"></el-option>
                                <el-option label="居住" value="居住"></el-option>
                                <el-option label="娱乐" value="娱乐"></el-option>
                                <el-option label="医疗" value="医疗"></el-option>
                                <el-option label="工资" value="工资"></el-option>
                                <el-option label="奖金" value="奖金"></el-option>
                                <el-option label="日用品" value="日用品"></el-option>
                                <el-option label="其他" value="其他"></el-option>
                            </el-select>
                        </el-tooltip>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item prop="paymentMethod" label="支付方式">
                        <el-tooltip content="请选择交易的支付方式" placement="top" effect="light">
                            <el-select v-model="form.paymentMethod" placeholder="请选择支付方式" style="width: 100%"
                                       size="small">
                                <el-option label="现金" value="现金"></el-option>
                                <el-option label="支付宝" value="支付宝"></el-option>
                                <el-option label="微信" value="微信"></el-option>
                                <el-option label="银行卡" value="银行卡"></el-option>
                                <el-option label="其他" value="其他"></el-option>
                            </el-select>
                        </el-tooltip>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item prop="createTime" label="创建时间">
                        <el-tooltip content="请选择交易记录的创建时间，默认为当前时间" placement="top" effect="light">
                            <el-date-picker
                                    v-model="form.createTime"
                                    type="datetime"
                                    placeholder="选择日期时间"
                                    style="width: 100%"
                                    size="small">
                            </el-date-picker>
                        </el-tooltip>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="dialogVisible = false" size="small">取 消</el-button>
                <el-button type="primary" @click="saveWallet" v-if="dialogType !== 'view'" size="small">确 定
                </el-button>
            </div>
        </template>
    </el-dialog>

    <!--导入对话框-->
    <el-dialog :visible.sync="importDialogVisible" title="导入交易记录" width="40%">
        <el-upload
                class="upload-demo"
                drag
                action="myWallet/import"
                :on-success="handleImportSuccess"
                :on-error="handleImportError"
                accept=".xls,.xlsx"
        >
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">
                拖拽文件到此处或 <em>点击上传</em>
            </div>
            <template #tip>
                <div class="el-upload__tip">
                    <el-tooltip content="支持.xls和.xlsx格式，请确保Excel表格格式正确" placement="top" effect="light">
                        <span>请上传Excel文件，且文件大小不超过500kb</span>
                    </el-tooltip>
                    <el-button size="small" type="text" @click="downloadTemplate" class="download-btn">
                        <i class="el-icon-download"></i> 下载导入模版
                    </el-button>
                </div>
            </template>
        </el-upload>
    </el-dialog>
</div>
</body>
<script src="static/js/owner/axios.min.js"></script>
<!-- import Vue before Element -->
<script src="static/js/owner/vue.js"></script>
<!-- import JavaScript -->
<script src="static/js/owner/vue-index.js"></script>
<script>
    new Vue({
        el: '#app',
        data: function () {
            return {
                tableData: [],// 表格数据
                total: 0,
                searchForm: {},// 搜索条件
                dialogVisible: false,// 对话框控制
                dialogType: 'add', // add, edit, view
                importDialogVisible: false,
                form: {// 表单数据
                    amount: 0,
                },
                // 表单验证规则
                rules: {
                    item: [
                        {required: true, message: "请输入事项描述", trigger: "blur"},
                        {min: 2, max: 100, message: "长度在 2 到 100 个字符", trigger: "blur"}
                    ],
                    amount: [
                        {required: true, message: "请输入金额", trigger: "blur"}
                    ],
                    type: [
                        {required: true, message: "请选择交易类型", trigger: "change"}
                    ],
                    category: [
                        {required: true, message: "请选择分类", trigger: "change"}
                    ],
                    paymentMethod: [
                        {required: true, message: "请选择支付方式", trigger: "change"}
                    ]
                }
            }
        },
        methods: {
            getList() {
                axios.get('myWallet/getData', {params: this.searchForm}).then(res => {
                    if (res.data.code === 200) {
                        this.tableData = res.data.extend.pageInfo.list;
                        this.total = res.data.extend.pageInfo.total;
                    } else {
                        this.$message.error('获取交易记录列表失败');
                    }
                })
            },
            // 搜索
            search() {
                this.searchForm.pageNum = 1;
                this.getList();
            },

            // 重置
            reset() {
                this.searchForm = {};
                this.getList();
            },
            // 新增记录
            add() {
                this.dialogType = 'add';
                this.dialogVisible = true;
                this.form = {
                    amount: 0,
                    createTime: new Date()
                };
                // 清空表单验证
                if (this.$refs.walletForm) {
                    this.$refs.walletForm.resetFields();
                }
                // 清空服务器验证错误
                this.serverValidErrors = {};
            },

            // 编辑记录
            handleEdit(row) {
                this.dialogType = 'edit';
                this.dialogVisible = true;
                // 清空服务器验证错误
                this.serverValidErrors = {};
                // 深拷贝row对象，避免直接修改表格数据
                this.form = JSON.parse(JSON.stringify(row));
            },

            // 查看记录
            handleView(row) {
                this.dialogType = 'view';
                this.dialogVisible = true;
                // 深拷贝row对象
                this.form = JSON.parse(JSON.stringify(row));
            },

            // 删除记录
            handleDelete(id) {
                axios.delete('myWallet/delete/' + id).then(res => {
                    if (res.data.code === 200) {
                        this.$message.success('删除成功');
                        this.getList();
                    } else {
                        this.$message.error(res.data.msg || '删除失败');
                    }
                });
            },

            // 保存记录
            saveWallet() {
                this.$refs.walletForm.validate(valid => {
                    if (valid) {
                        if (this.dialogType === 'add') {
                            // 新增
                            axios.post('myWallet/add', this.form).then(res => {
                                if (res.data.code === 200) {
                                    this.$message.success('添加成功');
                                    this.dialogVisible = false;
                                    this.getList();
                                } else if (res.data.extend && res.data.extend.errorFields) {
                                    // 处理服务器校验失败
                                    this.serverValidErrors.name = res.extend.errorFields.name;
                                    this.serverValidErrors.maker = res.extend.errorFields.maker;
                                    this.serverValidErrors.price = res.extend.errorFields.price;
                                    this.serverValidErrors.sales = res.extend.errorFields.sales;
                                    this.serverValidErrors.stock = res.extend.errorFields.stock;
                                } else {
                                    this.$message.error(res.data.msg || '添加失败');
                                }
                            });
                        } else {
                            // 编辑
                            axios.put('myWallet/update', this.form).then(res => {
                                if (res.data.code === 200) {
                                    this.$message.success('修改成功');
                                    this.dialogVisible = false;
                                    this.getList();
                                } else {
                                    this.$message.error(res.data.msg || '修改失败');
                                }
                            });
                        }
                    }
                });
            },

            // 导入记录
            importFurns() {
                this.importDialogVisible = true;
            },

            // 导出Excel
            exportFurn() {
                window.location.href = 'myWallet/exportExcel';
            },

            // 导入成功回调
            handleImportSuccess(response) {
                if (response.code === 200) {
                    this.$message.success(`成功导入${response.extend.successCount}条记录`);
                    this.importDialogVisible = false;
                    this.getList();
                } else {
                    this.$message.error(response.msg || '导入失败');
                }
            },

            // 导入失败回调
            handleImportError() {
                this.$message.error('文件上传失败');
            },

            // 下载导入模版
            downloadTemplate() {
                window.location.href = 'myWallet/downloadTemplate';
            },

            // 处理行点击
            handleRowClick(row) {
                this.$refs.walletTable.toggleRowSelection(row);
            },
            // 分页切换
            handleSizeChange(val) {
                this.searchForm.pageSize = val;
                this.getList();
            },

            handleCurrentChange(val) {
                this.searchForm.pageNum = val;
                this.getList();
            },
            // 表格单元格样式
            cellStyle() {
                return {
                    padding: '5px 0',
                };
            },
            // 表头样式
            headerCellStyle() {
                return {
                    padding: '0 0',
                };
            },
        },
        created() {
            this.getList();
        }
    })
</script>
<style>
    /* 基础样式 */
    html, body {
        margin: 0;
        padding: 0;
        height: 100%;
        overflow: hidden;
    }

    /* 主容器布局 */
    .wallet-container {
        display: flex;
        flex-direction: column;
        height: 100vh;
        overflow: hidden;
    }

    /* 搜索区域 */
    .search-area {
        padding: 15px 10px 5px 10px;/*上右下左*/
        background-color: #fff;
        box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
        flex-shrink: 0;
    }

    /* 功能按钮区域 */
    .function-area {
        padding: 5px;
        background-color: #fff;
        /*border-top: 1px solid #f0f0f0;*/
        flex-shrink: 0;
    }

    /* 表格区域 - 占用剩余所有空间并允许内部滚动 */
    .table-area {
        flex: 1;
        overflow: hidden;
        position: relative;
        padding: 0 0;/*上下 左右*/
    }

    /* 分页区域 */
    .pagination-area {
        padding: 15px 0;
        background-color: #fff;
        box-shadow: 0 -1px 4px rgba(0, 0, 0, 0.1);
        display: flex;
        justify-content: center;
        align-items: center;
        flex-shrink: 0;
        border-top: 1px solid #f0f0f0;
    }

    :last-child {
        margin-bottom: 0;
    }

    .operation-links {
        display: flex;
        justify-content: center;
        align-items: center;
    }

    .text-link {
        text-decoration: none;
        cursor: pointer;
        font-size: 14px;
    }

    .text-link.primary {
        color: #409EFF;
    }

    .text-link.danger {
        color: #F56C6C;
    }

    .text-link:hover {
        opacity: 0.8;
    }

    .divider {
        margin: 0 8px;
        color: #DCDFE6;
    }
</style>
